package mt.ctrl;

import java.util.List;
import java.util.Map;

import mt.model.Pricetype;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

import base.ctrl.BaseController;
import base.jfinal.JsonUtil;
import base.jfinal.anatation.RouteBind;

@RouteBind(path="pricetype")
public class PriceTypeCtrl extends BaseController{

	public void returnList(){
		List<Record> demoList = Db.find("select rateid id,minPlayer,maxPlayer,price from mt_theme_rate where themeId=? ",getParaToInt("themeId",0));
		JsonUtil ju = new JsonUtil(demoList);
		//这是一个纯List不允许put操作
		//如需一次返回多个List  参见 returnMultipleList
		this.renderJson(ju.toString());
	}
	@Before(Tx.class)
	public void modifyDemoList(){
		List<Map<String, Object>> objList = this.getParaToList("list");
		for (Map<String, Object> obj:objList){
			if (obj.get("action").equals("add")){
				obj.remove("id");
				obj.remove("action");
				obj.put("themeId", getParaToInt("themeId",0));
				obj.put("type", 1);
				(new Pricetype()).setAttrs(obj).save();
			}else if (obj.get("action").equals("del")){
				(new Pricetype()).deleteById(obj.get("id"));
			}else if (obj.get("action").equals("edit")){
				Pricetype demo = new Pricetype();
				obj.put("rateid", obj.get("id"));//设置Demo的主键=id的值
				obj.remove("id");
				obj.remove("action");
				demo.setAttrs(obj).update();
			}
		}
		//返回成功
		this.renderJson("{\"err\":0}");
	}
}
